//
//  HotelViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/19.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "HotelViewController.h"
#import "CityModel.h"
#import "PlayCell.h"
#import <CoreText/CoreText.h>
#import "DisCountWebView.h"


@interface HotelViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
@property (nonatomic, strong) NSMutableArray *hotelArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSString *cityStr;
@property (nonatomic, strong) UIImageView *bomView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation HotelViewController

// 视图的出现将tabbar隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)creatData {
    NSLog(@"lalala%@",self.hUrl);
 NSString *str = [[self.hUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]stringByReplacingOccurrencesOfString:@"%20" withString:@""];
    //由于转码后数据发生变化,
    NSLog(@"高兴%@",str);
    [ZSMAFNetWork geturl:str body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        NSLog(@"怎么妹纸%@",result);
       
        NSArray *arr = result[@"data"][@"hotel"];
        self.cityStr = result[@"data"][@"city_photo"];
      
        self.hotelArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            CityModel *city = [[CityModel alloc]initWithDic:dic];
            [self.hotelArr addObject:city];
        }
         [self.bomView sd_setImageWithURL:[NSURL URLWithString:self.cityStr] placeholderImage:[UIImage imageNamed:@"F"]];
       
        [self.tableView reloadData];
        self.hud.hidden = YES;
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
    
    [self creatData];
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.text = @"酒店";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = self.titleLabel;

    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 667) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = HEIGHT * 100;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self.hud hide:YES afterDelay:2];
   
    self.bomView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 200)];
    self.tableView.tableHeaderView = self.bomView;
    
    // Do any additional setup after loading the view.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.hotelArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"cell";
    
    PlayCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
         cell = [[PlayCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    
    CityModel *city = self.hotelArr[indexPath.row];
    [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:city.photo] placeholderImage:[UIImage imageNamed:@"F"]];
    cell.topLabel.text = city.cnname;
    cell.egLabel.text = city.enname;
    cell.starLabel.text = city.star;
    [cell.iconImage setImage:[UIImage imageNamed:@"iconfont-iconfontdingwei"]];
    cell.scoreLabel.text = [NSString stringWithFormat:@"%.1f分",city.ranking];
    cell.perNmuLabel.text = city.area_name;
    NSString *str = [NSString stringWithFormat:@"%@  元起  ",city.price];
    NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc]initWithString:str];
    //改变字体颜色;
    [attriString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
    //改变字体;
    [attriString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, 4)];
    [attriString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(4, 4)];
  
    cell.rightLabel.attributedText = attriString;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
      CityModel *city = self.hotelArr[indexPath.row];
    DisCountWebView *web = [[DisCountWebView alloc]init];
    web.disCountStr = city.link;
    [self.navigationController pushViewController:web animated:YES];
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.tableView.backgroundColor = [UIColor blackColor];
        self.titleLabel.textColor = [UIColor blackColor];
    }
    else {
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor whiteColor];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
